package com.miukoo.aikao.qmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.dtos.PageResDto;
import com.miukoo.aikao.qmanage.mapper.RepositoryMapper;
import com.miukoo.aikao.qmanage.pojos.Repository;
import com.miukoo.aikao.qmanage.dto.RepositoryDto;
import com.miukoo.aikao.qmanage.dto.response.RepositoryResDto;
import com.miukoo.aikao.qmanage.service.RepositoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
* 语言设置 服务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:23
*/
@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements RepositoryService {

    @Override
    public PageResDto<List<RepositoryResDto>> paging(PageReqDto<RepositoryDto> reqDto) {
        //创建分页对象
        Page page = new Page(reqDto.getCurrent(), reqDto.getSize());
        IPage<RepositoryResDto> paging = baseMapper.paging(page, reqDto.getParams());
        return PageResDto.result(paging.getTotal(),paging.getRecords());
    }

    @Override
    public void save(RepositoryDto reqDto) {
        //复制参数
        Repository entity = new Repository();
        BeanUtils.copyProperties(reqDto, entity);
        this.saveOrUpdate(entity);
    }

    @Override
    public void refreshStat(Long repoId) {
        baseMapper.refreshStat(repoId);
    }
    @Override
    public Long findByName(String name) {
        QueryWrapper<Repository> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Repository::getTitle, name);
        Repository repo = this.getOne(wrapper);

        if(repo!=null){
            return repo.getId();
        }
        return null;
    }
}
